from flask import Flask,session
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
import logging
from logging.handlers import RotatingFileHandler
from config import dict_config,Config

from redis import StrictRedis

'''
设置系统日志
'''
logging.basicConfig(level=logging.DEBUG)
file_log_handler = RotatingFileHandler('logs/log',maxBytes=1024*1024*100,backupCount=10)
formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')
file_log_handler.setFormatter(formatter)
logging.getLogger().addHandler(file_log_handler)

redis_store = StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_PORT)

db = SQLAlchemy()

def create_app(mode_config):
    app = Flask(__name__)
    app.config.from_object(dict_config[mode_config])
    from info.modules.news import news_blue
    from info.modules.passport import passport_blue
    app.register_blueprint(news_blue)
    app.register_blueprint(passport_blue)
    Session(app)
    db.init_app(app)

    return app

